package edu.avans.hartigehap.web.frontend.controller;

import java.util.Collection;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import edu.avans.hartigehap.domain.*;
import edu.avans.hartigehap.service.*;
import edu.avans.hartigehap.web.form.Message;

import org.springframework.web.servlet.mvc.support.*;

@RequestMapping("/restaurants/{restaurantName}")
@Controller
public class BillFrontendController {

	final Logger logger = LoggerFactory.getLogger(BillFrontendController.class);

	@Autowired
	private MessageSource messageSource;
	@Autowired
	private RestaurantService restaurantService;
	@Autowired
	private BillService billService;
	
	
	//private int internetTable;

	/*
	@RequestMapping(value = "diningTable", method = RequestMethod.GET)
	public String showTable(
			HttpServletRequest request,
			Model uiModel,
			
			@PathVariable("restaurantName") String restaurantName
			) {
		//logger.info("diningTable = " + diningTableId);
		//internetTable = 6; // Use Table 6 as internet Table

		// warmup stuff
		Collection<Restaurant> restaurants = restaurantService.findAll();
		uiModel.addAttribute("restaurants", restaurants);
		//DiningTable diningTable = diningTableService.fetchWarmedUp(Long.valueOf(internetTable));
		//uiModel.addAttribute("diningTable", null);
		Restaurant restaurant = restaurantService.fetchWarmedUp(restaurantName);
		uiModel.addAttribute("restaurant", restaurant);
		
		logger.info("menu meals = " + restaurant.getMenu().getMeals());
		logger.info("menu drinks = " + restaurant.getMenu().getDrinks());

		return "frontend/hartigehap/diningtable";
	}*/
	
	
	@RequestMapping(value = "menuItems/{menuItemName}", method = RequestMethod.POST)
	public String addMenuItem(
			HttpServletRequest request,
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName, 
			
			@PathVariable("menuItemName") String menuItemName
			) throws StateException {
		
		logger.info("menuItemName = " + menuItemName);

		Long billId = (Long) request.getAttribute("billId");
		logger.info("billId = " + billId);
		Bill bill = billService.findBillById(billId);
		logger.info("bill = " + bill);
		billService.addOrderItem(bill, menuItemName);
		
		return "redirect:/restaurants/"+restaurantName;
	}
	
	@RequestMapping(value = "menuItems/{menuItemName}", method = RequestMethod.DELETE)
	public String deleteMenuItem(
			HttpServletRequest request,
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName,
			 
			@PathVariable("menuItemName") String menuItemName
			) throws StateException {
		logger.info("menuItemName = " + menuItemName);
		
		Long billId = (Long) request.getAttribute("billId");
		logger.info("billId = " + billId);
		Bill bill = billService.findBillById(billId);
		logger.info("bill = " + bill);
		billService.deleteOrderItem(bill, menuItemName);

		return "redirect:/restaurants/"+restaurantName;
	}

	/*
	@RequestMapping(value = "", method = RequestMethod.PUT)
	public String receiveEvent(
			HttpServletRequest request,
			Model uiModel, 
			@PathVariable("restaurantName") String restaurantName,
			
			//@PathVariable("diningTableId") String diningTableId, 
			@RequestParam String event, 
			RedirectAttributes redirectAttributes,
			Locale locale
			) {
		//internetTable = 6;
		
		//logger.info("(receiveEvent) diningTable = " + diningTableId);

		// warmup stuff
		Collection<Restaurant> restaurants = restaurantService.findAll();
		uiModel.addAttribute("restaurants", restaurants);
		//DiningTable diningTable = diningTableService.fetchWarmedUp(Long.valueOf(internetTable));
		//uiModel.addAttribute("diningTable", diningTable);
		Restaurant restaurant = restaurantService.fetchWarmedUp(restaurantName);
		uiModel.addAttribute("restaurant", restaurant);
		//Bill bill = restaurantService.fetchWarmedUp(billId);
		//uiModel.addAttribute("restaurant", restaurant);
		
		// because of REST, the "event" parameter is part of the body. It therefore cannot be used for
		// the request mapping so all events for the same resource will be handled by the same
		// controller method; so we end up with an if statement
		
		// as of JDK 7, this can be a String-based switch statement, which is better
		if(event.equals("submitBill")) {
			try {
				InternetBill bill = (InternetBill) billService.findBillById( (Long) request.getAttribute("billId"));
				billService.submitBill(bill);
			} catch(EmptyBillException e) {
				uiModel.addAttribute("message", new Message("error", messageSource.getMessage("message_submit_empty_bill_fail", new Object[]{}, locale))); 
				return "frontend/hartigehap/bill";
			} catch(StateException e) {
				uiModel.addAttribute("message", new Message("error", messageSource.getMessage("message_submit_bill_fail", new Object[]{}, locale))); 

				// StateException triggers a rollback; consequently all Entities are invalidated by Hibernate
				// So new warmup needed
				// warmup stuff
				restaurants = restaurantService.findAll();
				uiModel.addAttribute("restaurants", restaurants);
				//diningTable = diningTableService.fetchWarmedUp(Long.valueOf(internetTable));
				//uiModel.addAttribute("diningTable", diningTable);

				return "frontend/hartigehap/bill";
			}
			// store the message temporarily in the session to allow displaying after redirect
			redirectAttributes.addFlashAttribute("message", new Message("success",
					messageSource.getMessage("message_submit_bill_success", new Object[]{}, locale))); 
			return "redirect://restaurants/"+restaurantName;
			
		} 
		
		else {
			logger.error("internal error: event " + event + "not recognized");
			return "frontend/hartigehap/listrestaurants";			
		}
		
		
	}*/
}
